package com.cuit.qx.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cuit.qx.bean.Message;
import com.cuit.qx.bean.PageList;
import com.cuit.qx.bean.User;
import com.cuit.qx.service.MessageService;
import com.github.pagehelper.PageHelper;

/**
 * 消息管理
 * @author Administrator
 *
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/api/message")
@Controller
public class MessageController {
	
	@Autowired
	private MessageService messageService;
	
	/**
	 * 新增/更新消息通知
	 * @param message
	 * @return
	 */
	@RequestMapping("/insertOrUpdateMessage")
	@ResponseBody
	public Map<String,Object> insertMessage(HttpServletRequest request,@RequestBody Message message){
		Map<String,Object> result=new HashMap<String,Object>();
		Date date = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String format2 = format.format(date);
		String rString = ","+message.getNotice_user()+",";
		message.setNotice_user(rString);
		HttpSession session = request.getSession();
		User userInfo = (User) session.getAttribute("user");
		message.setMessage_time(format2);
		message.setAuthor(userInfo.getUsername());
		message.setState(0);
		message.setConfirm_user("");
		if(message.getMessage_id()==null) {
			message.setMessage_id(UUID.randomUUID().toString().replaceAll("-", ""));
			int insert = messageService.insertMessage(message);
			if (insert>0) {
				result.put("success", true);
			}else {
				result.put("success", false);
			}
		}else {
			int updateMessage = messageService.updateMessage(message);
			if (updateMessage>0) {
				result.put("success", true);
			}else {
				result.put("success", false);
			}
		}
        Map<String, Object> result1 = new HashMap<String, Object>();
        result1.put("code", 20000);
        result1.put("data", result);
		return result1;

	}
	
	/**
	 * 获取新增消息条数
	 * @param request
	 * @return
	 */
	@RequestMapping("/getCountMessage")
	@ResponseBody
	public Map<String,Object> getCountMessage(HttpServletRequest request){
		Map<String,Object> result=new HashMap<String,Object>();
		User user = (User) request.getSession().getAttribute("user");
		String session=user.getUsername();
		if(user.getUsername()==null) {
			session="admin";
		}
		int countMessage = messageService.getCountMessage(session);
		result.put("count", countMessage);
        Map<String, Object> result1 = new HashMap<String, Object>();
        result1.put("code", 20000);
        result1.put("data", result);
		return result1;
	}
	
	/**
	 * 获取消息管理列表
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("/getList")
	@ResponseBody
	public Map<String, Object> getList(Integer page,Integer rows){
		if (page != null && rows != null)
			PageHelper.startPage(page, rows);
		List<Message> list=messageService.getList();
		PageList<Message> pageList = new PageList<Message>(list);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("code", 20000);
        result.put("data", pageList);
		return result;
	}
	
	/**
	 * 获取新消息列表
	 * @return
	 */
	@RequestMapping("/getNewList")
	@ResponseBody
	public Map<String, Object> getNewList(HttpServletRequest request){
		User user=(User) request.getSession().getAttribute("user");
		String session=user.getUsername();
		if(user.getUsername()==null) {
			session="admin";
		}
		List<Message> list=messageService.getNewList(session);
        Map<String, Object> result1 = new HashMap<String, Object>();
        result1.put("code", 20000);
        result1.put("data", list);
		return result1;
	}
	
	/**
	 * 删除消息
	 * @param request
	 * @return
	 */
	@RequestMapping("/deleteMessage")
	@ResponseBody
	public Map<String,Object> deleteMessage(String id){
		Map<String,Object> result=new HashMap<String,Object>();
		int deleteMessage = messageService.deleteMessage(id);
		if(deleteMessage>0) {
			result.put("success", true);
		}else {
			result.put("success", false);
		}
	    Map<String, Object> result1 = new HashMap<String, Object>();
	    result1.put("code", 20000);
	    result1.put("data", result);
	    return result1;

	}	
	
	/**
	 * 确认消息
	 * @param request
	 * @return
	 */
	@RequestMapping("/confirmMessage")
	@ResponseBody
	public Map<String,Object> confirmMessage(HttpServletRequest request){
		Map<String,Object> result=new HashMap<String,Object>();
		String id=request.getParameter("id");
		User user=(User) request.getSession().getAttribute("user");
		String session=user.getUsername();
		if(user.getUsername()==null) {
			session="admin";
		}
		int confirmMessage = messageService.confirmMessage(id,session);
		if(confirmMessage>0) {
			result.put("success", true);
		}else {
			result.put("success", false);
		}
	    Map<String, Object> result1 = new HashMap<String, Object>();
	    result1.put("code", 20000);
	    result1.put("data", result);
	    return result1;

	}	


}
